package com.house.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.house.common.BaseContext;
import com.house.entity.Menu;
import com.house.entity.Role;
import com.house.mapper.RoleMapper;
import com.house.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int save(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public int update(Role role) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleId, role.getRoleId());
        return roleMapper.update(role, queryWrapper);
    }

    @Override
    public Page<Role> selectPage(int page, int pageSize, String roleName) {
        Page<Role> pageInfo= new Page<>(page, pageSize);
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getCreateUser, BaseContext.getThreadUserId());
        queryWrapper.like(roleName != null, Role::getRoleName, roleName);
        queryWrapper.orderByAsc(Role::getCreateTime);
        return roleMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public Role getRoleByRoleId(int roleId) {
        return roleMapper.selectRoleByRoleId(roleId);
    }

    @Override
    public Role getRoleMenuIdsByRoleId(int roleId) {
        return roleMapper.selectRoleMenuIdsByRoleId(roleId);
    }

    @Override
    public int delete(int roleId) {
        return roleMapper.delete(roleId);
    }

    @Override
    public int bindMenus(Role role) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleId, role.getRoleId());
        return roleMapper.update(role, queryWrapper);
    }
}
